const {
  openLuckDraw,   //打开抽奖页面
  addLuckDrawNum,   //视频增加抽奖次数
  startLuckDraw,    //开始抽奖
  endLuckDraw,    //抽奖结束，更新奖品
} = require('../lib/action')

module.exports = function (app) { return new Handler(app); };
let Handler = function (app) { this.app = app; };
let handler = Handler.prototype;

//打开抽奖页面
handler.openLuckDraw = async function (msg, session, next){
  let { uid } = session;
  try{
    //返回抽奖次数
    let luckDrawNum = await openLuckDraw.call(this, uid) || 0;
    next(null, {code: 200, luckDrawNum});
  }catch(err){
    next(err, {code: 500, msg: err.message});
  }
}

//视频增加抽奖次数
handler.addLuckDrawNum = async function (msg, session, next) {
  let { uid } = session;
  try{
    // 增加抽奖次数
    await addLuckDrawNum.call(this, uid);
    next(null, {code: 200 });
  }catch(err){
    next(err, {code: 500, msg: err.message});
  }
}

//开始抽奖，返回奖品
handler.startLuckDraw = async function (msg, session, next) {
  try{
    // 返回奖品
    let {type, scale, num_str} = await startLuckDraw.call(this, session);
    next(null, {code: 200, type, scale, num_str});
  }catch(err){
    next(err, {code: 500, msg: err.message});
  }
}

//抽奖结束，更新奖品
handler.endLuckDraw = async function (msg, session, next) {
  try{
    // 返回奖品
    await endLuckDraw.call(this, session);
    next(null, { code: 200 });
  }catch(err){
    next(err, {code: 500, msg: err.message});
  }
}
